//
//  HMOriginalStatusView.swift
//  WeiBo14
//
//  Created by HM on 16/3/6.
//  Copyright © 2016年 HM. All rights reserved.
//  首页原创微博的视图,原创微博里面的所有内容都展示在这个视图里面

import UIKit
import SnapKit
import YYText

/**
 if let urlString = statusViewModel?.status?.user?.profile_image_url {
    if let url = NSURL(string: urlString) {
        headImage.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
    }
 }
*/

class HMOriginalStatusView: UIView {
    
    // 当前视图(原创微博)底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: HMStatusViewModel? {
        didSet{
            // 设置显示的头像
            if let urlString = statusViewModel?.status?.user?.profile_image_url, let url = NSURL(string: urlString){
                headImage.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
            }
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 设置时间和来源 TODO: 以后会专门处理
            timeLabel.text = statusViewModel?.createAtText
            sourceLabel.text = statusViewModel?.sourceText
            // 设置认证图标
            verifiedImage.image = statusViewModel?.verifiedImage
            // 设置vip图标
            membershipImage.image = statusViewModel?.membershipImage
            // 设置内容
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedString
            
            // 设置内容大小
            contentLabel.snp_updateConstraints { (make) -> Void in
                print(statusViewModel!.originalAttrSize!)
                make.size.equalTo(statusViewModel!.originalAttrSize!)
            }
            
            // 卸载掉之前的约束
            // 1. 取到之前的约束
            // 2. 卸载
            self.bottomConstraint?.uninstall()
            
            // TODO: 根据是否有配图去显示或者隐藏配图视图
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
                // 有配图数据
                // 0. 设置数据
                pictrueView.picUrls = picUrls
                
                // 1. 显示配图视图的控件
                pictrueView.hidden = false
                // 2. 更改当前原创微博View的底部约束为配图视图的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictrueView).offset(HMStatusCellMargin).constraint
                })
            }else{
                // 没有配图数据
                
                // 1. 隐藏配图视图的控件
                pictrueView.hidden = true
                // 2. 更改当前原创微博View的底部约束为内容视图(contentLabel)的底部
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
         backgroundColor = UIColor.whiteColor()
        
        // 添加子控件
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImage)
        addSubview(membershipImage)
        addSubview(contentLabel)
        addSubview(pictrueView)
        
        // 添加约束
        
        headImage.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: HMStatusCellHeadImageWH, height: HMStatusCellHeadImageWH))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(headImage)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImage)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_trailing).offset(-3)
            make.centerY.equalTo(headImage.snp_bottom).offset(-3)
        }
        
        membershipImage.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(HMStatusCellMargin)
            // make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        // 添加配图视图约束
        pictrueView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            // 暂时宽高写死
            // make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 添加当前视图的底部等于 pictrueView 的底部加上一个间距.在没有设置配图视图之前.是等于contentLabel底部
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictrueView).offset(HMStatusCellMargin).constraint
        }
        contentLabel.highlightTapAction = { (_, attr, range: NSRange, _) in
            print("高亮文字点击了")
            // 取到对应位置的属性
            let attribute = attr.yy_attribute(YYTextHighlightAttributeName, atIndex: UInt(range.location)) as? YYTextHighlight
            // 怎么才能取到点击的 YYTextHighLighted
            print(attribute?.userInfo["text"])
            
        }
        
    }
    
    // MARK: - 懒加载控件
    
    // 头像
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    // 昵称
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        label.text = "隔壁老王"
        return label
    }()
    
    // 微博创建时间的label
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
        label.text = "1小时前"
        return label
    }()
    
    // 微博来源的label
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 12)
        label.text = "来自 黑马微博"
        return label
    }()
    
    // 认证
    private lazy var verifiedImage: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "avatar_vip")
        return imageView
    }()
    
    // 会员图标
    private lazy var membershipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    // 显示微博文字的label
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        label.font = UIFont.systemFontOfSize(HMStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
//        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusContentFontSize)
//        label.numberOfLines = 0
//        return label
    }()
    
    // 原创微博的配图视图
    private lazy var pictrueView: HMStatusPictureView = {
        let picture = HMStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        picture.backgroundColor = self.backgroundColor
        return picture
    }()
}
